package com.pj.wisdomlogistics.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.Toast;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.TakeoutMenuInfo;
import com.pj.wisdomlogistics.ui.view.CountView;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HelloWorld on 2017/9/12.
 */

public class TakeOutPastaAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private Context mContext;
    private List<TakeoutMenuInfo.MianshiBean> mList;

    public static Map<Integer, Integer> map ;
    public static boolean isCanShop;

    public TakeOutPastaAdapter(Context mContext) {
        this.mContext = mContext;
        isCanShop = true;
        map = new HashMap<>();
    }

    public void setData(List<TakeoutMenuInfo.MianshiBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(parent, R.layout.item_takeout_menu);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        AutoUtils.autoSize(holder.itemView);

        holder.getTextView(R.id.tv_dishes_name).setText(mList.get(position).getFoodName());
        holder.getTextView(R.id.tv_dishes_price).setText(mList.get(position).getFoodPrice() + "");
        holder.getTextView(R.id.tv_dishes_unit).setText("元/" + mList.get(position).getFoodUnit());

        if(position % 2 != 0){
            holder.getView(R.id.ll_layout).setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        final CountView mCountView = holder.getView(R.id.v_count);

        mCountView.setListener(new CountView.countListener() {
            @Override
            public void countChange(int currentCount) {
                mCountListener.itemClick(
                        mList.get(position).getFoodID(),
                        currentCount,
                        mList.get(position).getFoodName(),
                        mList.get(position).getFoodPrice(),
                        mList.get(position).getFoodUnit(),
                        Cons.TakeoutMeal.PASTA);
            }
        }, true);
        mCountView.setMaxCount(mList.get(position).getMaxCount());

        mCountView.setIsCanListener(new CountView.isCanShopListener() {
            @Override
            public void isCanShop(int currentCount, int type) {
                if (map.size() == 2 && type == 1) {
                    if (map.get(mList.get(position).getFoodID()) != null) {
                        map.put(mList.get(position).getFoodID(), currentCount);
                        mCountView.addCount();
                    } else {
                        Toast.makeText(mContext, "每人每单最多点两种面食", Toast.LENGTH_SHORT).show();
                    }
                    return;
                }

                if (map.size() < 2 && type == 1) {
                    map.put(mList.get(position).getFoodID(), currentCount);
                    mCountView.addCount();
                    return;
                }

                if (map.get(mList.get(position).getFoodID()) != null && currentCount == 0 && type == -1) {
                    map.remove(mList.get(position).getFoodID());
                    return;
                }
            }
        });


    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public countListener mCountListener;

    public interface countListener {
        public void itemClick(int id, int currentCount, String name, double price, String unit , int type);
    }

    public void setListener(countListener mListener) {
        this.mCountListener = mListener;
    }

}
